package com.fz.us.oa.web.controller.com;

import com.fz.us.admin.company.entity.Remind;
import com.fz.us.admin.company.entity.TaskEntity;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.company.service.DutyService;
import com.fz.us.admin.company.service.RemindService;
import com.fz.us.admin.company.service.TaskEntityService;
import com.fz.us.oa.core.entity.com.AllChange;
import com.fz.us.oa.core.entity.com.Formal;
import com.fz.us.oa.core.entity.com.PersonnelInfo;
import com.fz.us.oa.core.entity.com.PersonnelUse;
import com.fz.us.oa.core.service.com.AllChangeService;
import com.fz.us.oa.core.service.com.FormalService;
import com.fz.us.oa.core.service.com.PersonnelInfoService;
import com.fz.us.oa.core.service.com.PersonnelUseService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZhuChunXiao on 2017/7/18.
 */
@Controller
@RequestMapping(value = "/com/formal")
public class FormalController extends BaseController {
    private static final long serialVersionUID = 8750722710366033745L;

    @Autowired
    private FormalService formalService;
    @Autowired
    private PersonnelInfoService personnelInfoService;
    @Autowired
    private AllChangeService allChangeService;
    @Autowired
    private PersonnelUseService personnelUseService;
    @Autowired
    private TaskEntityService taskEntityService;
    @Autowired
    private DutyService dutyService;
    @Autowired
    private RemindService remindService;

    /**
     * 新建，编辑页面
     */
    @RequestMapping(value = "input", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String input(@CurrentUser User user, Model model,String keyId,String parentId,String draft,String index,String todo,String remind,String record) {
        if (StringUtils.isNotEmpty(keyId)){
            Formal formal=formalService.get(keyId);
            model.addAttribute("keyId", keyId);
            model.addAttribute("formal", formal);
        }
        PersonnelInfo personnelInfo=personnelInfoService.get(parentId);
        model.addAttribute("parentId", parentId);
        model.addAttribute("personnelInfo", personnelInfo);
        model.addAttribute("draft", draft);
        model.addAttribute("index", index);
        model.addAttribute("todo", todo);
        model.addAttribute("remind", remind);
        model.addAttribute("record", record);
        return "/com/formal_input";
    }

    /**
     * 只读页面
     */
    @RequestMapping(value = "read", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String read(@CurrentUser User user, Model model,String keyId,String draft,String index,String todo,String remind,String record) {
        if(StringUtils.isNotEmpty(keyId)) {
            Formal formal=formalService.get(keyId);
            TaskEntity taskEntity = taskEntityService.getInfoByKeyId(keyId);
            int stepNo = taskEntity.getStepNo();
            String isEdit =  isEdit(keyId);
            model.addAttribute("isEdit", isEdit);
            model.addAttribute("formal", formal);
            model.addAttribute("stepNo", stepNo);
        }
        model.addAttribute("keyId", keyId);
        model.addAttribute("draft", draft);
        model.addAttribute("index", index);
        model.addAttribute("todo", todo);
        model.addAttribute("remind", remind);
        model.addAttribute("record", record);
        return "/com/formal_read";
    }

    /**
     * 从异动总汇进入只读页面
     */
    @RequestMapping(value = "toRead",method = RequestMethod.GET,produces = MediaType.TEXT_HTML_VALUE)
    public String toRead(@CurrentUser User user, Model model,String allChangeId,String keyId,String draft,String index,String todo,String remind,String record){
        AllChange allChange=allChangeService.get(allChangeId);
        Formal formal=allChange.getFormal();
        TaskEntity taskEntity = taskEntityService.getInfoByKeyId(formal.getId());
        int stepNo = taskEntity.getStepNo();
        String isEdit =  isEdit(formal.getId());
        model.addAttribute("isEdit", isEdit);
        model.addAttribute("formal", formal);
        model.addAttribute("stepNo", stepNo);
        model.addAttribute("keyId", keyId);
        model.addAttribute("draft", draft);
        model.addAttribute("index", index);
        model.addAttribute("todo", todo);
        model.addAttribute("remind", remind);
        model.addAttribute("record", record);

        return "/com/formal_read";
    }

    /**
     * 保存Model数据
     */
    public Formal saveModel(User user ,String keyId, String parentId, String opinion){
        Formal formal=null;
        if(StringUtils.isBlank(keyId)){
            formal=new Formal();
            formal.setCreater(user);
            formal.setCompany(user.getCompany());
            formal.setName("转正申请");
        }else{
            formal=formalService.get(keyId);
        }
        if(StringUtils.isNotEmpty(opinion)){
            formal.setOpinion(opinion);
        }
        if(StringUtils.isNotEmpty(parentId)){
            formal.setPersonnelInfo(personnelInfoService.get(parentId));
        }

        return formal;
    }

    /**
     * 保存操作
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Object save(@CurrentUser User user,
                       @RequestParam(required = false) String keyId,
                       String parentId,
                       String opinion){

        Map<String,Object> rMap=new HashMap<String,Object>();
        Formal formal=saveModel(user, keyId, parentId, opinion);

        if(StringUtils.isNotEmpty(keyId)){
            formalService.update(formal);
            rMap.put("message", "更新成功");
        } else {
            formalService.save(formal, "com.fz.us.oa.core.entity.com.Formal");
            rMap.put("message", "保存成功");
        }
        rMap.put("state", "1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

    /**
     * 提交操作
     */
    @RequestMapping(value = "submit", method = RequestMethod.POST)
    public Object submit(@CurrentUser User user,
                         @RequestParam(required = false) String keyId,
                         String parentId,
                         String opinion,
                         String curDutyId,
                         String comment) {

        Map<String, Object> rMap = new HashMap<String, Object>();
        Formal formal=saveModel(user,keyId,parentId,opinion);

        if (StringUtils.isNotEmpty(keyId)) {
            formalService.approve(formal, dutyService.get(curDutyId), comment);
        } else {
            formalService.commit(formal, "com.fz.us.oa.core.entity.com.Formal", dutyService.get(curDutyId));
        }

        //新建异动总汇表
        AllChange allChange=new AllChange();
        allChange.setType("1");
        allChange.setFormal(formal);
        allChange.setPersonnelInfo(personnelInfoService.get(parentId));
        allChange.setName("转正申请");
        allChange.setCreater(user);
        allChange.setCompany(user.getCompany());
        allChange.setIsFinished("0");
        allChangeService.commit(allChange, "com.fz.us.oa.core.entity.com.AllChange", dutyService.get(curDutyId));

        formal.setAllChange(allChange);
        formalService.update(formal);

        if(formal.getProcessState().name().equals("Finished")){
            doFinished(formal,parentId);
        }

        rMap.put("message", "提交成功");
        rMap.put("state", "1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

    //审批
    @RequestMapping(value = "approve", method = RequestMethod.POST)
    public Object approve(@CurrentUser User user, String keyId,  String curDutyId, String comment, String parentId){
        Map<String,Object> rMap=new HashMap<String,Object>();
        Formal formal=formalService.get(keyId);
        formalService.approve(formal, dutyService.get(curDutyId), comment);

        //归档后...
        if(formal.getProcessState().name().equals("Finished")){
            doFinished(formal,parentId);
        }
        rMap.put("message","提交成功");
        rMap.put("state","1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

    public void doFinished(Formal formal,String parentId){
        //完成总汇表
        AllChange allChange=formal.getAllChange();
        allChange.setIsFinished("1");
        allChangeService.update(allChange);

        //修改人事信息的用工性质
        PersonnelInfo personnelInfo=personnelInfoService.get(parentId);
        personnelInfo.setNature(1);
        personnelInfoService.saveEntity(personnelInfo);

        //修改用工信息的用工性质
        PersonnelUse personnelUse=personnelInfo.getPersonnelUse();
        personnelUse.setNature(1);
        personnelUseService.update(personnelUse);

        //发送提醒
        Remind remind = new Remind();
        remind.setUser(personnelInfoService.get(parentId).getUser());
        remind.setBussinessKey("formal");
        remind.setKeyId(formal.getId());
        remind.setContent("您的转正申请已通过，请查看");
        remindService.save(remind);
    }

    /**
     * 退回
     * @return
     */
    @RequestMapping(value = "back", method = RequestMethod.POST)
    public Object back(@CurrentUser User user, String keyId,String curDutyId, String comment) {
        if (StringUtils.isNotEmpty(keyId)) {
            Formal formal=formalService.get(keyId);
            formalService.back(formal, dutyService.get(curDutyId), comment);
        }
        Map<String,Object> rMap=new HashMap<String,Object>();
        rMap.put("message","退回成功");
        rMap.put("state","1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }
    /**
     * 否决
     * @return
     */
    @RequestMapping(value = "deny", method = RequestMethod.POST)
    public Object deny(@CurrentUser User user, String keyId,String curDutyId, String comment) {
        if (StringUtils.isNotEmpty(keyId)) {
            Formal formal=formalService.get(keyId);
            formalService.deny(formal, dutyService.get(curDutyId), comment);
        }
        Map<String,Object> rMap=new HashMap<String,Object>();
        rMap.put("message","否决成功");
        rMap.put("state","1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

}
